package spring.superrun.record.mvc.service.impl;

import spring.superrun.record.mvc.entity.SuperrunManager;
import spring.superrun.record.mvc.dao.SuperrunManagerDao;
import spring.superrun.record.mvc.entity.enumlibs.ServiceResultCode;
import spring.superrun.record.mvc.entity.response.ServiceResult;
import spring.superrun.record.mvc.service.SuperrunManagerService;
import org.springframework.stereotype.Service;
import spring.superrun.record.mvc.setup.CookieConfig;
import spring.superrun.tool.newid.Encrypt;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * (SuperrunManager)表服务实现类
 *
 * @author makejava
 * @since 2021-06-15 21:16:43
 */
@Service("superrunManagerService")
public class SuperrunManagerServiceImpl implements SuperrunManagerService {
    @Resource
    private SuperrunManagerDao superrunManagerDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SuperrunManager queryById(String id) {
        return this.superrunManagerDao.queryById(id);
    }

    /**
     * 根据用户名查询一个管理员
     * @param userName
     * @return
     */
    @Override
    public SuperrunManager queryByUserName(String userName) {
        return this.superrunManagerDao.queryByUserName(userName);
    }

    /**
     * 验证管理员账号和密码
     * @param userName 用户名
     * @param Password  密码
     * @return 验证成功的管理员
     */
    @Override
    public SuperrunManager VerityManagerByUserNameAndPassword(String userName
            , String Password) {
        if (userName.isEmpty()||Password.isEmpty()){
            return  null;
        }
        SuperrunManager manager = queryByUserName(userName);
        if (null==manager){
            return null;
        }
        return Encrypt.authenticatePassword(manager.getPasswordhash(),Password)
                ?manager:null;
    }
    /**
     * 登录接口
     * @param username 用户名
     * @param password 密码
     * @param session
     * @param request
     * @param response
     * @return
     */
    @Override
    public ServiceResult Login(String username, String password, HttpSession session,
                                  HttpServletRequest request, HttpServletResponse response){
        // 最终返回的对象
        ServiceResult res = new ServiceResult();
        res.IsFailed("登录失败!");
        if (username.isEmpty() || password.isEmpty()) {
            res.IsFailed("请输入用户名或密码");
            return res;
        }
        SuperrunManager manager =VerityManagerByUserNameAndPassword(username,password);
        if (null == manager) {
            res.IsFailed("该账号不存在或密码错误，请检查后重试");
            return res;
        }
        // 判断账户状态
        if (1 != manager.getEnabled()) {
            res.IsFailed("该账号已被冻结，请联系管理员");
            return res;
        }
        // 将登录用户信息保存到session中
        session.setAttribute(CookieConfig.SYSTEM_USER_SESSION, manager);
        // 保存cookie，实现自动登录
        Cookie cookie_username = new Cookie(CookieConfig.SYSTEM_USER_SESSION, manager.getId());
        // 设置cookie的持久化时间，30天 30 * 24 * 60 * 60
        cookie_username.setMaxAge(60*60);//一个小时
        // 设置为当前项目下都携带这个cookie
        cookie_username.setPath(request.getContextPath());
        // 设置路径
        cookie_username.setPath("/");
        // 域名
        cookie_username.setDomain("localhost");
        // 向客户端发送cookie--将cookie写入浏览器 - Response
        response.addCookie(cookie_username);
        res.IsSuccess("登录成功");
        return res;
    }

    /**
     * 注册新用户
     * @param userName
     * @param password
     * @return
     */
    public ServiceResult Join(String userName,String password){
        ServiceResult res = new ServiceResult();
        res.IsFailed("注册失败！");
        if (userName.isEmpty()||password.isEmpty()){
            res.IsFailed("用户或密码不能为空!");
            return  res;
        }
        SuperrunManager manager = queryByUserName(userName);
        if (null!=manager){
            res.IsFailed(ServiceResultCode.Repeat,"用户名已重复");
            return res;
        }
        manager =new SuperrunManager();
        SuperrunManager managerAdd =insert(manager.Add(userName,password));
        if (managerAdd == null){
            return res;
        }
        res.IsSuccess("注册成功");
        return res;
    }
    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<SuperrunManager> queryAllByLimit(int offset, int limit) {
        return this.superrunManagerDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param superrunManager 实例对象
     * @return 实例对象
     */
    @Override
    public SuperrunManager insert(SuperrunManager superrunManager) {
        this.superrunManagerDao.insert(superrunManager);
        return superrunManager;
    }

    /**
     * 修改数据
     *
     * @param superrunManager 实例对象
     * @return 实例对象
     */
    @Override
    public SuperrunManager update(SuperrunManager superrunManager) {
        this.superrunManagerDao.update(superrunManager);
        return this.queryById(superrunManager.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.superrunManagerDao.deleteById(id) > 0;
    }
}
